﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FytSoa.Common;
using FytSoa.Core.Model.Sw;
using FytSoa.Extensions;
using FytSoa.Service.DtoModel;
using FytSoa.Service.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FytSoa.Api.Controllers.Shop
{
    [Route("api/shop/[controller]")]
    [ApiController]
    [JwtAuthorize(Roles = "Admin")]
    public class SpectController : ControllerBase
    {
        private readonly ISw_typesService _typeService;
        private readonly ISw_spectService _spectService;
        private readonly ISw_spectcodeService _codeService;
        private readonly ICmsColumnService _classService;
        public SpectController(ISw_typesService typeService
            , ISw_spectService spectService
            , ISw_spectcodeService codeService
            , ICmsColumnService classService)
        {
            _typeService = typeService;
            _spectService = spectService;
            _codeService = codeService;
            _classService = classService;
        }

        #region 规格类别
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet("type/list")]
        public async Task<IActionResult> TypeList([FromQuery]PageParm param)
        {
            return Ok(await _typeService.GetPageAsync(param));
        }

        /// <summary>
        /// 根据唯一编号查询实体
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("type/model")]
        public async Task<IActionResult> TypeModel([FromBody]ParmString obj)
        {
            var siteGuid = SiteTool.CurrentSite?.Guid;
            var dblist = await _classService.GetListAsync(m => m.SiteGuid == siteGuid && m.TypeID == 2, m => m.Sort, DbOrderEnum.Asc);
            var list = _classService.RecursiveModule(dblist.data);
            foreach (var item in list)
            {
                item.Title = Utils.LevelName(item.Title, item.ClassLayer);
            }
            var columnModel = await _typeService.GetModelAsync(m => m.Guid == obj.parm);
            var gx = await _typeService.GetAllBrandAndType();
            var resDto = new GetSpectTypeDto()
            {
                model = columnModel.data,
                list = list,
                brand=gx.data.brand,
                spect=gx.data.spect
            };
            return Ok(new ApiResult<GetSpectTypeDto>()
            {
                data = resDto,
                statusCode = columnModel.statusCode,
                message = columnModel.message
            });
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("type/add")]
        public async Task<IActionResult> TypeAdd([FromBody]Sw_types model)
        {
            model.Guid = Guid.NewGuid().ToString();
            return Ok(await _typeService.AddAsync(model));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("type/update")]
        public async Task<IActionResult> TypeUpdate([FromBody]Sw_types model)
        {
            return Ok(await _typeService.UpdateAsync(model));
        }

        /// <summary>
        /// 删除类别
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost("type/delete")]
        public async Task<IActionResult> TypeDelete([FromBody]ParmString obj)
        {
            return Ok(await _typeService.DeleteAsync(obj.parm));
        }

        #endregion

        #region 规格栏目
        /// <summary>
        /// 获得规格类别列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("category/list")]
        public async Task<IActionResult> CategoryList()
        {
            return Ok(await _spectService.GetListAsync(m=>true,m=>m.Sort,DbOrderEnum.Desc));
        }

        /// <summary>
        /// 根据编号查询一条规格栏位信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost("category/model")]
        public async Task<IActionResult> GetCategoryModel([FromBody]ParmString obj)
        {
            return Ok(await _spectService.GetModelAsync(m => m.Guid == obj.parm));
        }

        /// <summary>
        /// 添加规格栏目
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("category/add")]
        public async Task<IActionResult> CategoryAdd([FromBody]Sw_spect model)
        {
            model.Guid = Guid.NewGuid().ToString();
            return Ok(await _spectService.AddAsync(model));
        }

        /// <summary>
        /// 修改规格栏目
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("category/update")]
        public async Task<IActionResult> CategoryUpdate([FromBody]Sw_spect model)
        {
            return Ok(await _spectService.UpdateAsync(model));
        }

        /// <summary>
        /// 删除规格栏目
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost("category/delete")]
        public async Task<IActionResult> CategoryDelete([FromBody]ParmString obj)
        {
            return Ok(await _spectService.DeleteAsync(obj.parm));
        }
        #endregion

        #region 规格值
        /// <summary>
        /// 查询规格值-分页
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpGet("code/list")]
        public async Task<IActionResult> GetSpectCodePage([FromQuery]PageParm param)
        {
            return Ok(await _codeService.GetPagesAsync(param,m=>m.SpectGuid==param.guid,m=>m.Sort,DbOrderEnum.Desc));
        }

        /// <summary>
        /// 根据编号查询一条规格值信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost("code/model")]
        public async Task<IActionResult> GetCodeModel([FromBody]ParmString obj)
        {
            return Ok(await _codeService.GetModelAsync(m=>m.Guid==obj.parm));
        }

        /// <summary>
        /// 添加规格值
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("code/add")]
        public async Task<IActionResult> CodeAdd([FromBody]Sw_spectcode model)
        {
            model.Guid = Guid.NewGuid().ToString();
            return Ok(await _codeService.AddAsync(model));
        }

        /// <summary>
        /// 修改规格值
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("code/update")]
        public async Task<IActionResult> CodeUpdate([FromBody]Sw_spectcode model)
        {
            return Ok(await _codeService.UpdateAsync(model));
        }

        /// <summary>
        /// 删除规格值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost("code/delete")]
        public async Task<IActionResult> CodeDelete([FromBody]ParmString obj)
        {
            return Ok(await _codeService.DeleteAsync(obj.parm));
        }
        #endregion
    }
}